<?php
set_time_limit(0);

include_once('lib/config.php');
include_once('lib/Db.class.php');
include_once('lib/Model.class.php');
include_once('lib/Upyun.class.php');
include_once('lib/Image.class.php');
include_once('lib/Http.class.php');
include_once('lib/Cut.class.php');
include_once('lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$model = new Model;
$http  = new Http;
$cut   = new Cut;

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||mh_zcopy_chapter_update');

$limit = 50;
while($limit--) {
  $stop = $redis->get('LNPAN_MH_QUEUE_STOP');
  if($stop) {
    die('stop');
  }

  $data  = array();

  $where = "news_state=9 ";
  //前10个里随机取一个
  $rand  = rand(0,5);
  $limit = "$rand, 1";

  $info = $model->find('news_zcopy', $where, 'uptime asc, id asc', $limit);
  $model->close();
  if($info) {
    $url = $info['link'];
    $parse_url = parse_url($url);
    $opt_data = array(
      CURLOPT_REFERER => $url,
      CURLOPT_ENCODING=> 'gzip',
      CURLOPT_HTTPHEADER => array(
        'Referer' => $url,
        'Host' => $parse_url['host'],
        'Pragma' => 'no-cache',
      ),
    );
    $http->opt = $opt_data;
    $responses = $http->curl_http( array($url) );
    $file = $responses[$url]['results'];
    $data = $cut->getData($file, $info);
    $data['http_code'] = $responses[$url]['info']['http_code'].' '.$_SERVER['REMOTE_ADDR'];
    $data['link'] = $url;

    if(empty($data['title2'])) {
      $data['msg'] = '抓取失败请重试';
      if($info['chapter_count'] == 0) {
        $updata = array('state'=>3, 'news_state'=>0);
        $model->save("news_zcopy", "id='$info[id]'", $updata);
      }
    }
    elseif($info['chapter_count'] != $data['chapter_count'] && $data['chapter_count'] > $info['chapter_count']) {
      $data['state'] = $data['status']=='完结' ? 1 : 0;
      $data['chapter_count_old'] = $info['chapter_count'];
      $data['chapter'] = json_encode($data['chapter']);
      $data['chapter_update'] = json_encode($data['chapter_update']);
      $data['info'] = $data['info'] ? $data['info'] : strip_tags($info['info']);
      $data['list_state'] = 0; //有更新
      
      $model->save("news_zcopy", "id='$info[id]'", $data);
      $data['msg'] = '新章节已更新';
    }
    else {
      $data['msg'] = '无更新';
      $data['chapter_count_old'] = $info['chapter_count'];
      $data['chapter'] = json_encode($data['chapter']);
      $data['chapter_update'] = json_encode($data['chapter_update']);
      
      $updata = array();
      if($info['chapter_count'] == 0) {
        $updata = array(
          'state' => 2,
        );
      } elseif(!empty($data['chapter']) && $data['chapter'] != 'null') {
        $updata = array(
          'chapter' => $data['chapter'],
          'chapter_update' => $data['chapter_update'],
          'news_state' => 1,
        );
      }

      $model->save("news_zcopy", "id='$info[id]'", $updata);
    }

    $updata = array('uptime'=>time());
    $model->save("news_zcopy", "id='$info[id]'", $updata);
    $model->close();

    $data['id'] = $info['id'];
  }
  else {
    $data['msg'] = 'finished';
    sleep(5);
  }

  $data = print_r($data, true);
  $redis->lPush('LOG_ZCOPY_CHAPER_UPDATE', $data);
  usleep(1000*100);
  //print_r($data);
  //exit;
}

